package com.season.act;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;

import java.text.ParseException;
import java.util.List;


public class CrutService {

    private ProcessEngine processEngine= ProcessEngines.getDefaultProcessEngine();

    public void add(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("newAct.bpmn")
                .name("yuepao")
                .deploy();
        System.out.println("部署ID"+deployment.getId());

    }

    public void del(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        repositoryService.deleteDeployment("12501",true);
    }
    public void finddef(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("myProcess_1")
                .list();
        for (ProcessDefinition definition : list) {
            System.out.println(definition.getId()+definition.getDeploymentId()+definition.getVersion());
        }
    }
    public void finddefid(){
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId("myProcess_1:2:10004")
                .singleResult();
        System.out.println("流程信息:"+processDefinition.getId()+"\t"+processDefinition.getDeploymentId()+"\t"+processDefinition.getVersion());
    }
    public void start(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("myProcess_1");
        System.out.println(instance.getProcessDefinitionId());

    }
    public void findHis(){
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .list();
        for (HistoricTaskInstance ht : list) {
            System.out.println(ht.getId()+"\t"+ht.getName()+"\t"+ht.getStartTime()+"\t"+ht.getEndTime());
        }
    }
    //按照时间查询
    public void findHisTime() throws ParseException {
        String start = "2018-09-01";
        String end = "2018-09-30";
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
                .taskCreatedAfter(DateUtils.stringToDate(start))
                .taskCreatedBefore(DateUtils.stringToDate(end))
                .list();
        for (HistoricTaskInstance ht : list) {
            System.out.println(ht.getId()+"\t"+ht.getName()+"\t"+ht.getStartTime()+"\t"+ht.getEndTime());
        }
    }
    public void findExe(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("10001")
                .singleResult();
        if(processInstance!=null){
            System.out.println("流程在运行中");
        }else{
            System.out.println("流程没有运行");
        }
    }

    public static void main(String[] args) {
        CrutService crutService=new CrutService();
        //crutService.add();
        //crutService.del();
        //crutService.finddef();
        //crutService.finddefid();
        //crutService.start();
        //crutService.findHis();
//        try {
////            crutService.findHisTime();
////        } catch (ParseException e) {
////            e.printStackTrace();
////        }
        crutService.findExe();
    }
}
